{% extends "manage/layout.html" %}

{% set page_title = '机构列表' %}

{% block content %}
<style>
.tab{
    position: absolute;
    margin-left: 40px;
    margin-top: 10px;
}
</style>
<div class="tab">
    <a type="button" class="btn btn-primary" href="{{ url_for('manage.list_schools') }}">资料视图</a>
    <a type="button" class="btn btn-primary btn-success" href="{{ url_for('manage.schools_operation_list') }}">运营视图</a>
    招生满三个班：该机构的学生数量大于等于18人; 招生不满：小于18人
</div>
<div class="m-content">
    <div class="table-top">
        <a type="button" class="btn btn-default{% if filter_type==2 %} btn-success{% endif %}" href="{{ url_for('manage.schools_operation', filter_type=2) }}">已安装客户名单</a>
        <a type="button" class="btn btn-default{% if filter_type==3 %} btn-success{% endif %}" href="{{ url_for('manage.schools_operation', filter_type=3) }}">已参加培训客户名单</a>
        <a type="button" class="btn btn-default{% if filter_type==4 %} btn-success{% endif %}" href="{{ url_for('manage.schools_operation', filter_type=4) }}">招生满三个班的客户名单</a>
        <a type="button" class="btn btn-default{% if filter_type==5 %} btn-success{% endif %}" href="{{ url_for('manage.schools_operation', filter_type=5) }}">不满三个班的客户名单</a>
        {# 暂时去掉该功能 COS-926
            <a type="button" class="btn btn-primary" href="javascript:;" onclick="location.href='{{ url_for('manage.export_schools_operation_excel') }}'">导出Excel</a>
        #}
        <div class="search_form pull-right" style="margin-right:40px;">
            <form class="form-inline" action="{{ url_for('manage.search') }}" method="GET">
                <div class="form-group">
                    <input type="text" class="form-control" style="width: 330px" id="keywords" name="keywords" placeholder="机构名称、代码、地址、联系人、手机号" value="{% if request.args.get('keywords') %}{{ request.args.get('keywords') }}{% endif %}"/>
                </div>
                <button type="submit" class="btn btn-default">搜索</button>
            </form>
        </div>
    </div>
    <table data-toggle="table" class="table table-striped table-condensed table-bordered">
        <thead>
            <tr>
                <th data-sortable="true">序号</th>
                <th>机构名称</th>
                <th>学生数量</th>
                <th data-sortable="true">类型</th>
                <th data-sortable="true">创建于</th>
                <th>教室管理(绿已安装，红未安装）</th>
                <th>班级数量</th>
                <th>活跃班级数量</th>
                <th>是否培训</th>
            </tr>
        </thead>
        <tbody>
            {% if schools %}
                {% for school in schools %}
                <tr id="{{ school.id }}">
                    <td>{{ school.id }}</td>
                    <td>
                        {{ school.name }}<br />
                        <span class="school-code">{{ school.code }}</span>
                    </td>
                    <td>{{ school.students_number }}</td>
                    <td>{{ school.school_type }}</td>
                    <td>{{ school.created_at }}</td>
                    <td>
                        {%- if school.classrooms %}
                            {% for cr in school.classrooms %}
                                {% if cr.devices.count() %}
                                    {% set classroom_color = 'btn-success' %}
                                {% else %}
                                    {% set classroom_color = 'btn-danger' %}
                                {% endif %}
                                <a style="width:36px;" class="btn btn-sm {{ classroom_color }}" href="{{ url_for('manage.list_classrooms_by_sid', school_id=school.id) }}">{{ cr.capacity }}</a>
                            {% if not loop.index % 4 %}
                            <br /> {% endif %}
                            {% endfor %}
                        {% else %}
                        <a style="width:45px;color:#393;" class="btn btn-sm btn-danger" href="javascript:;">未找到教室</a>
                        {% endif %}
                    </td>
                    <td>
                        {{ school.classes_number }}
                    </td>
                    <td>
                        {{ school.active_classes_number }}
                    </td>
                    <td>
                        <input type="checkbox" class="is_trained"{% if current_user.has_permission('SET_TRAINED_STATUS') %}{% if school.is_trained %} checked{% endif %}{% else %} disabled="disabled"{% endif %}>
                    </td>
                </tr>
                {% endfor %}
            {% endif %}
        </tbody>
    </table>
</div>
{% if pagination %}
    {% include "manage/pagination.html" %}
{% endif %}
<div class="modal">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">确认删除</h4>
            </div>
            <div class="modal-body">
                <button class="btn btn-danger">确认</button>
                <button class="btn btn-default">取消</button>
            </div>
        </div>
    </div>
</div>
{% endblock %}
{% block js %}
<script type="text/javascript">
function update_trained(school_id, is_trained){
    url = '/manage/schools_update_trained/' + school_id
    $.post(url,
            {is_trained: is_trained},
            function(data){
                if (data['error']!=0){
                    alert(data['message']);
                }
            }, 'json');
}
$(function(){
    // click trash
    var muti_delete = false;
    var school_id;

    $('.is_trained').change(function(){
        school_id = $(this).parents('tr').attr('id');
        is_trained = $(this).is(':checked');
        if(!is_trained){
            var result = confirm('请确认要设置该机构为“未培训”！');
            if(result){
                update_trained(school_id, is_trained);
            }else{
                $(this).prop("checked", true);
            }
        }else{
            update_trained(school_id, is_trained);
        }
    });
});
</script>
{% endblock %}
